package com.xx.news.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xx.news.R;
import com.xx.news.adapter.NewsAdapter;
import com.xx.news.entity.NewEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FragmentNews extends Fragment {
    private RecyclerView mRecyclerView;
    private static NewsAdapter mAdapter;
    private static List<NewEntity.News> mList;
    private String type;

    String apiKey = "3b60161c0f7d8693a03ce3a22198603c";
    String apiUrl = "http://v.juhe.cn/toutiao/ ";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public FragmentNews(String type) {
        this.type = type;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_counter_dialog1, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new NewsAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addItemClickListener(new NewsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(NewEntity.News item) {
//                Intent intent = new Intent(getActivity(), NewsInfoActivity.class);
//                intent.putExtra("bean", item);
//                startActivity(intent);
            }
        });
//        request();
    }

//    public void request() {
//        //创建Retrofit对象
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(apiUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        //创建网络请求接口的实例
//        ApiService apiService = retrofit.create(ApiService.class);
//        //对发送送请求进行封装
//        Call<NewEntity> call = apiService.getIndex(this.apiKey, this.type, 1, 30, 0);
//        //发送网络请求(异步)
//        call.enqueue(new Callback<NewEntity>() {
//            @Override
//            public void onResponse(Call<NewEntity> call, Response<NewEntity> response) {
//                try {
//                    NewEntity entity = response.body();
//                    if (entity.error_code == 0) {
//                        Log.e("jjjjjj", "onResponse: " + entity.error_code + "=====" + Thread.currentThread().getName());
//                        mAdapter.updateData(entity.result.data);
//                    } else {
//                        Toast.makeText(getActivity(), entity.reason, Toast.LENGTH_LONG).show();
//                    }
//                } catch (Exception e) {
//
//                }
//                //                mList = entity.result;
//                //这里是子线程不能更新UI
//                //                handler.sendEmptyMessage(10);
//            }
//
//            @Override
//            public void onFailure(Call<NewEntity> call, Throwable t) {
//                Log.i("onFailure", "请求失败");
//            }
//        });
//    }

}
